package com.wing.visual.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.DateUtils;
import com.wing.platform.order.model.entity.Warehouse;
import com.wing.visual.dao.WarehouseDao;
import com.wing.visual.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * 仓库可视化
 *
 * @author xiayang
 */
@Service
@Slf4j
public class WarehouseServiceImpl extends ServiceImpl<WarehouseDao, Warehouse> implements WarehouseService {

    @Resource
    WarehouseDao warehouseDao;

    @Override
    public List<WarehouseMaterialAmountTotal> warehouseMaterialAmountTotal() {
        List<WarehouseMaterialAmountTotal> list = warehouseDao.warehouseMaterialAmountTotal();
        BigDecimal amountSum = list.stream().
                filter(a-> ObjectUtil.isNotEmpty(a.getAmountTotal())).
                map(WarehouseMaterialAmountTotal::getAmountTotal).
                reduce(BigDecimal.ZERO, BigDecimal::add);
        for (WarehouseMaterialAmountTotal warehouseMaterialAmountTotal : list) {
            if (ObjectUtil.isNotEmpty(warehouseMaterialAmountTotal.getAmountTotal()) && warehouseMaterialAmountTotal.getAmountTotal().compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal divide = warehouseMaterialAmountTotal.getAmountTotal()
                        .divide(amountSum, 4, RoundingMode.HALF_UP);
                warehouseMaterialAmountTotal
                        .setRate(divide);
            }else{
                warehouseMaterialAmountTotal.setRate(BigDecimal.ZERO);
            }
        }
        return list;
    }

    @Override
    public Map<String, Object> inventoryMaterialTotal() {
        Map<String, Object> result = new HashMap<>();
        //库存物资总额
        BigDecimal warehouseAmountSum = warehouseDao.warehouseAmountSum();
        result.put("warehouseAmountSum", warehouseAmountSum);
        //出库任务数
        Integer warehouseOutStorageTaskSum = warehouseDao.warehouseOutStorageTaskSum();
        result.put("warehouseOutStorageTaskSum", warehouseOutStorageTaskSum);
        //物流数量
        Integer logisticsSum = warehouseDao.logisticsSum();
        result.put("logisticsSum", logisticsSum);
        //入库任务数
        Integer warehouseInStorageTaskSum = warehouseDao.warehouseInStorageTaskSum();
        //出库作业统计
        Map<String, Object> params = new HashMap<>();
        params.put("inventoryType", 2);
        Integer warehouseOutStorageQuantityTotal = warehouseDao.warehouseInoutStorageQuantityTotal(params);
        //出入库作业统计
        Integer warehouseInoutStorageQuantityTotal = warehouseDao.warehouseInoutStorageQuantityTotal(null);
        //发货计划完成率
        BigDecimal deliveryPlanRate = new BigDecimal(warehouseOutStorageQuantityTotal).divide(new BigDecimal(warehouseOutStorageTaskSum), 2, BigDecimal.ROUND_HALF_UP);
        result.put("deliveryPlanRate", deliveryPlanRate);
        //完结率
        BigDecimal finishRate = new BigDecimal(warehouseInoutStorageQuantityTotal).divide(new BigDecimal(warehouseOutStorageTaskSum + warehouseInStorageTaskSum), 2, BigDecimal.ROUND_HALF_UP);
        result.put("finishRate", finishRate);
        return result;
    }

    @Override
    public List<WarehouseTypeTotal> warehouseTypeTotal() {
        return warehouseDao.warehouseTypeTotal();
    }

    @Override
    public List<WarehouseInoutStorageTotal> warehouseInoutStorageTotal(Map<String, Object> params) {
        List<WarehouseInoutStorageTotal> list = warehouseDao.warehouseInoutStorageTotal(params);
        for (WarehouseInoutStorageTotal warehouseInoutStorageTotal : list) {
            warehouseInoutStorageTotal.setInoutStorageQuantityTotal(list.stream().mapToInt(WarehouseInoutStorageTotal::getInoutStorageQuantity).sum());
            warehouseInoutStorageTotal.setRate(new BigDecimal(warehouseInoutStorageTotal.getInoutStorageQuantity()).divide(new BigDecimal(warehouseInoutStorageTotal.getInoutStorageQuantityTotal()), 4, BigDecimal.ROUND_HALF_UP));
        }
        return list;
    }

    @Override
    public List<WarehouseInoutStorageOrder> inoutStorageOrderTotal() {
        List<WarehouseInoutStorageOrder> resultList = new ArrayList<>();
        Map<String, Object> params = new HashMap<>();
        params.put("startTime", DateUtils.getDateByDay(new Date(), -6));
        params.put("endTime", DateUtils.getDateByDay(new Date(), 0));
        List<WarehouseInoutStorageOrderTotal> list = warehouseDao.inoutStorageOrderTotal(params);
        //时间间隔天数
        Integer dayNum = DateUtils.getTimeDay(params.get("startTime").toString(), params.get("endTime").toString());
        for (int i = dayNum; i >= 0; i--) {
            Date endTime = DateUtils.stringFormatDate(params.get("endTime").toString(), DateUtils.DATE_PATTERN);
            String inoutStorageDate = DateUtils.getDateByDay(endTime, i == 0 ? 0 : -i);
            WarehouseInoutStorageOrder wiot = new WarehouseInoutStorageOrder();
            wiot.setInoutStorageDate(inoutStorageDate);
            //入库
            WarehouseInoutStorageOrderTotal inStorageTotal = list.stream().filter(t -> t.getInoutStorageDate().equals(inoutStorageDate) && t.getInventoryType() == 1).findFirst().orElse(null);
            if (inStorageTotal == null) {
                wiot.setInNum(0);
            } else {
                wiot.setInNum(inStorageTotal.getInoutNum());
            }
            //出库
            WarehouseInoutStorageOrderTotal outStorageTotal = list.stream().filter(t -> t.getInoutStorageDate().equals(inoutStorageDate) && t.getInventoryType() == 2).findFirst().orElse(null);
            if (outStorageTotal == null) {
                wiot.setOutNum(0);
            } else {
                wiot.setOutNum(outStorageTotal.getInoutNum());
            }
            resultList.add(wiot);
        }
        return resultList;
    }

}
